package cloud.seri.iot.aliyun

import com.aliyuncs.DefaultAcsClient
import com.aliyuncs.iot.model.v20180120.*

class DeviceTopoManager(private val client: DefaultAcsClient)
{


    /**
     * 查询网关设备或子设备所具有的拓扑关系。
     *
     * @param IotId      设备所隶属的产品Key
     * @param ProductKey 产品Key
     * @param DeviceName 设备的名称
     * @param PageSize   返回结果中每页显示的记录数量。数量限制：每页最多可显示50条记录。            必须
     * @param PageNo     设备标签键值            必须
     * @Des 描述：
     */
    fun getThingTopo(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null,
                     PageSize: Int, PageNo: Int): GetThingTopoResponse?
    {

        val request = GetThingTopoRequest().apply {
            deviceName = DeviceName
            iotId = IotId
            pageNo = PageNo
            pageSize = PageSize
            productKey = ProductKey
        }
        return client.getAcsResponse(request)

    }

    /**
     * 根据挂载的子设备信息，查询对应的网关设备信息
     *
     * @param IotId      要查询的设备ID  必须
     * @param ProductKey 设备所隶属的产品Key  必须
     * @param DeviceName 设备的名称列表。最多支持500个设备  必须
     * @Des 描述：
     */
    fun getGatewayBySubDevice(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null): GetGatewayBySubDeviceResponse?
    {

        val request = GetGatewayBySubDeviceRequest().apply {
            iotId = IotId
            deviceName = DeviceName
            productKey = ProductKey
        }

        return client.getAcsResponse(request)
    }

    /**
     * 网关设备增加拓扑关系。
     *
     * @param GwIotId       设备所隶属的产品Key
     * @param GwProductKey  网关设备所隶属的产品Ke
     * @param GwDeviceName  要通知的网关设备的名称，即网关类型设备的DeviceName
     * @param DeviceListStr 要挂载在目标网关设备上的子设备数组，为JSON字符串形式
     * @Des 描述：
     */
    fun notifyAddThingTopo(GwIotId: String, GwProductKey: String, GwDeviceName: String,
                           DeviceListStr: String): NotifyAddThingTopoResponse?
    {

        val request = NotifyAddThingTopoRequest().apply {
            deviceListStr = DeviceListStr
            gwDeviceName = GwDeviceName
            gwIotId = GwIotId
            gwProductKey = GwProductKey
        }
        return client.getAcsResponse(request)
    }

    /**
     * 移除网关与子设备的拓扑关系
     *
     * @param IotId
     * @param ProductKey
     * @param DeviceName
     * @Des 描述：
     */
    fun removeThingTopo(IotId: String, ProductKey: String, DeviceName: String): RegisterDeviceResponse?
    {
        val request = RegisterDeviceRequest()
        request.productKey = ProductKey
        request.deviceName = DeviceName
        return client.getAcsResponse(request)
    }
}
